package com.simple.datax.common.util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ObjectCheckUtil {

	public static boolean isNotNull(String src) {
		if (src == null || src.trim().equals("")) {
			return false;
		}
		return true;
	}

//	public static boolean checkMaxLength(String src, int maxLength)
//			throws FineException {
//		try {
//			if (src == null || src.getBytes("GBK").length <= maxLength) {
//				return true;
//			}
//			return false;
//		} catch (UnsupportedEncodingException e) {
//			throw FineException.getInstance(new DataCheckMessage(
//					DataCheckMessageEnum.ENCODING_ERROR, "GBK"));
//		}
//	}
//
//	public static boolean checkLength(String src, int length)
//			throws FineException {
//		try {
//			if (src != null && src.getBytes("GBK").length == length) {
//				return true;
//			}
//			return false;
//		} catch (UnsupportedEncodingException e) {
//			throw FineException.getInstance(new DataCheckMessage(
//					DataCheckMessageEnum.ENCODING_ERROR, "GBK"));
//		}
//	}

	public static boolean isInteger(String src) {
		if (src == null || src.trim().equals("")) {
			return false;
		}
		Pattern pattern = Pattern.compile("[+]?\\d+");
		Matcher matcher = pattern.matcher(src);
		return matcher.matches();
	}

	public static boolean isDecimal(String src, int maxLength, int decimal) {
		if (src == null || src.trim().equals("")) {
			return false;
		}
		String rule = "[+]?\\d{0," + (maxLength - decimal) + "}(\\.\\d{0,"
				+ decimal + "})?";
		Pattern pattern = Pattern.compile(rule);
		Matcher matcher = pattern.matcher(src);
		return matcher.matches();
	}

	public static boolean isPlusDecimal(String src, int maxLength, int decimal) {
		if (src == null || src.trim().equals("")) {
			return false;
		}
		String rule = "\\d{0," + (maxLength - decimal) + "}(\\.\\d{0,"
				+ decimal + "})?";
		Pattern pattern = Pattern.compile(rule);
		Matcher matcher = pattern.matcher(src);
		return matcher.matches();
	}

	public static boolean isDate(String src) {
		if (src == null || src.trim().equals("")) {
			return false;
		}
		Pattern pattern = Pattern
				.compile("^((((1[6-9]|[2-9]\\d)\\d{2})(0[13578]|1[02])(0[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})(0[13456789]|1[012])(0[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})0?2(0[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))0?229))((20|21|22|23|[0-1]\\d)[0-5]\\d[0-5]\\d)?$");
		Matcher matcher = pattern.matcher(src);
		return matcher.matches();
	}

	public static boolean isCode(String src) {
		if (src == null || src.trim().equals("")) {
			return false;
		}
		Pattern pattern = Pattern.compile("\\w+");
		Matcher matcher = pattern.matcher(src);
		return matcher.matches();
	}

	public static boolean isAllowValue(String src, String[] allowValue) {
		if (allowValue == null || !(allowValue.length > 0)) {
			return true;
		}
		if (src == null || src.equals("")) {
			for (int i = 0; i < allowValue.length; i++) {
				if (allowValue[i] == null || allowValue[i].equals("")) {
					return true;
				}
			}
		}
		for (int i = 0; i < allowValue.length; i++) {
			if (src.equals(allowValue[i])) {
				return true;
			}
		}

		return false;
	}

}
